# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   ApiTest
# FileName:      read_basic_ini.py
# Author:       liu
# Datetime:     2024/9/7 上午9:30
# Description: 读取基础ini文件
# ---------------------------------------------------------------------------
import os
from configparser import ConfigParser

from ApiTest.report import log


class Read_basic_ini:
    def __init__(self):
        # config_data的路径
        self.main_path = os.path.dirname(os.path.dirname(__file__))
        data_path = os.path.join(self.main_path, 'config_data')
        # print(data_path)
        # ini 路径
        ini_path = os.path.join(data_path, 'basic_config.ini')
        # print(ini_path)
        # 初始化
        self.config = ConfigParser()
        # 读取文件
        self.config.read(ini_path, encoding='utf-8')

    def get_sql(self, key):
        try:
            # log.info(f"执行方法get_sql为：根据key获取sql节点下key对应文件的路径，形参key的传参为{key},")
            key_value = self.config.get('sql', key)
            return key_value
        except Exception as e:
            log.error('值错误，没有查询到值，请检查代码')
            raise e

    def get_host(self, key):
        try:
            key_value = self.config.get('host', key)
            return key_value
        except Exception as e:
            log.error('值错误，没有查询到值，请检查代码')
            raise e

    def get_report(self, key):
        try:
            # 返回路径
            report_path = os.path.join(self.main_path, 'report')
            key_path = os.path.join(report_path, self.config.get('report', key))
            # 判断是否存在，不存在则创建一个文件夹，无法创建则报错说路径不对
            if os.path.exists(key_path):
                pass
            else:
                os.makedirs(key_path)
        except Exception as e:
            # # log.error(f'{key_path}错误，请检查路径是否正确且为文件夹，错误为{e}')
            log.error(f"方法get_file_path执行失败，形参key传参为：{key},错误为：{e}")
            raise e


if __name__ == '__main__':
    read_ini = Read_basic_ini()
    print(read_ini.get_sql('host'))
    print(read_ini.get_host('bpm_host'))
    print(read_ini.get_report('log1'))